package com.lovo.csc.service.impl;

import com.alibaba.fastjson.JSON;
import com.lovo.csc.entity.Ding;
import com.lovo.csc.entity.Purchase;
import com.lovo.csc.mapper.DingMapper;
import com.lovo.csc.service.IDingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.csc.service.IPurchaseService;
import com.lovo.sscweb.dto.OrderDto;
import com.lovo.sscweb.dto.OrderItemTo;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lovo
 * @since 2022-07-12
 */
@Service
@Slf4j
public class DingServiceImpl extends ServiceImpl<DingMapper, Ding> implements IDingService {

    @Autowired
    IDingService service;
    @Autowired
    IPurchaseService purchasenoeService;
    @RabbitListener(queues = MQUtil.ORDERQUEUE2)
    public void order(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, String orderInfo) throws IOException {
        System.out.println(orderInfo);
        if(null!=orderInfo){
            OrderDto dto= JSON.parseObject(orderInfo,OrderDto.class);
            Ding order = new Ding();
            order.setDOrdercode(dto.getOrderCode());
            order.setDAddress(dto.getShippingAddress());
            order.setDUsername(dto.getUsername());
            order.setDTime(dto.getPlaOrderTime());
            order.setDZprice(dto.getOrderTotal());
            order.setDDiscount(dto.getDiscount());
            order.setDSname(dto.getOrderConsignee());
            log.info(dto+"");
            System.out.println(order);
            service.save(order);
            List<OrderItemTo> list=dto.getGoodsList();
            for (OrderItemTo orderItemTo:list){
                Purchase purchase=new Purchase();
                purchase.setPBuynum(orderItemTo.getBuyNum());
                purchase.setFkName(order.getDUsername());
                purchase.setPImg(orderItemTo.getImg());
                purchase.setPGoodsprice(orderItemTo.getGoodsPrice());
                purchase.setPGoodsname(orderItemTo.getGoodsName());
                log.info(orderInfo+"");
                purchasenoeService.save(purchase);
            }
            }

        channel.basicAck(tag,false);



    }
}